package com.lulalei.dbviewer.commons.enums;

import com.lulalei.dbviewer.commons.exception.CustomizeException;

/**
 * 数据库列数据类型枚举类
 * @author lulalei
 */
public enum RowTypeEnums {
    VARCHAR("varchar","String"),
    CHAR("char","String"),
    BLOB("blob","byte[]"),
    TEXT("text","String"),
    INTEGER("integer","Long"),
    INT("int","Long"),
    TINYINT("tinyint","Integer"),
    SMALLINT("smallint","Integer"),
    MEDIUMINT("mediumint","Integer"),
    BIT("bit","Integer"),
    BIGINT("bigint","BigInteger"),
    FLOAT("float","Float"),
    DOUBLE("double","Double"),
    DECIMAL("decimal","BigDecimal"),
    DATE("date","Date"),
    TIME("time","Date"),
    DATETIME("datetime","Date"),
    TIMESTAMP("timestamp","Date"),;

    private final String jdbcType;

    private final String javaType;

    RowTypeEnums(String jdbcType, String javaType) {
        this.jdbcType = jdbcType;
        this.javaType = javaType;
    }

    public String getJdbcType() {
        return jdbcType;
    }

    public String getJavaType() {
        return javaType;
    }

    public static RowTypeEnums getInstance(String typeName) throws CustomizeException {
        RowTypeEnums[] values = RowTypeEnums.values();
        for (RowTypeEnums value : values) {
            typeName = typeName.toLowerCase();
            if (typeName.contains(value.jdbcType)){
                return value;
            }
        }
        throw new CustomizeException("无法解析的数据库字段类型");
    }

}
